package org.example.what_mall.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.what_mall.dto.UserLoginForm;
import org.example.what_mall.dto.UserSignupForm;
import org.example.what_mall.entity.User;
import org.example.what_mall.result.HttpResult;
import org.example.what_mall.service.IUserService;
import org.example.what_mall.vo.UserDataVO;
import org.example.what_mall.vo.UserVO;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "用户接口")
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public HttpResult<UserVO> login(@RequestBody UserLoginForm form, HttpServletResponse response) {
        UserVO userVO = userService.login(form);
        response.setHeader("token", userVO.getToken());
        return HttpResult.success(userVO);
    }

    @Operation(summary = "普通用户注册")
    @PostMapping("/signup")
    public HttpResult<UserVO> signup(@RequestBody UserSignupForm form, HttpServletResponse response) {
        UserVO userVO = userService.signup(form);
        response.setHeader("token", userVO.getToken());
        return HttpResult.success(userVO);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/data")
    public HttpResult<UserDataVO> userData(@RequestParam("token") String token) {
        return HttpResult.success(userService.userData(token));
    }

    @Operation(summary = "更新用户信息")
    @PostMapping("/updateUserData")
    public HttpResult<Void> updateUserData(@RequestBody User vo) {
        userService.updateUserData(vo);
        return HttpResult.ok();
    }
}
